package com.zhanbo.zhanbowymusic.main.podcast;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.SeekBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mwx.baselib.utils.MLog;
import com.mwx.baselib.utils.PixelUtil;
import com.zhanbo.zhanbowymusic.R;
import com.zhanbo.zhanbowymusic.databinding.FragmentPodcastBinding;
import com.mwx.baselib.base.BaseFragment;
import com.zhanbo.zhanbowymusic.main.featured.adapter.FeaturedHomeHotPodcastListAdapter;
import com.zhanbo.zhanbowymusic.main.podcast.adapter.PodcastTitleListAdapter;
import com.zhanbo.zhanbowymusic.main.podcast.bean.PodcastTitleListBean;
import com.zhanbo.zhanbowymusic.play.MediaPlayerUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 播客fragment
 */
public class PodcastFragment extends BaseFragment<FragmentPodcastBinding> implements View.OnClickListener {


    private ArrayList<PodcastTitleListBean> podcastTitleListBeanList;
    private PodcastTitleListAdapter podcastTitleListAdapter;
    private int firstVisibleItemPosition;
    private int maindx;
    private LinearLayoutManager linearLayoutManager;
    private int centerPosition;

    @Override
    protected void init(Bundle savedInstanceState) {
        initView();
        initListener();

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {

        super.onLazyInitView(savedInstanceState);


    }

    @Override
    protected Object getLayout() {
        MLog.e("R.layout.fragment_podcast");

        return R.layout.fragment_podcast;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
//        // 创建一个Drawable对象，指定thumb的图像
//        Drawable thumbDrawable = getResources().getDrawable(R.mipmap.bokehuadong);
//        // 设置thumb的边界
//        thumbDrawable.setBounds(0, 550, 0, 50);
//        // 设置SeekBar的thumb
//        binding.seekbarBoke.setThumb(thumbDrawable);




         linearLayoutManager =new LinearLayoutManager(_mActivity, LinearLayoutManager.HORIZONTAL, false);
        binding.rvPodcastTitle.setLayoutManager(linearLayoutManager);
        podcastTitleListAdapter = new PodcastTitleListAdapter(_mActivity);
        PodcastTitleListBean podcastTitleListBean01 = new PodcastTitleListBean("       ", false, R.color.color_C29913);
        PodcastTitleListBean podcastTitleListBean02 = new PodcastTitleListBean("       ", false, R.color.color_C29913);
        PodcastTitleListBean podcastTitleListBean = new PodcastTitleListBean("真实故事", false, R.color.color_C29913);
        PodcastTitleListBean podcastTitleListBean1 = new PodcastTitleListBean("新闻资讯", false, R.color.color_FD3E4A);
        PodcastTitleListBean podcastTitleListBean2 = new PodcastTitleListBean("历史解密", true, R.color.color_36B285);
        PodcastTitleListBean podcastTitleListBean3 = new PodcastTitleListBean("搞笑段子", false, R.color.color_text);
        PodcastTitleListBean podcastTitleListBean4 = new PodcastTitleListBean("影视娱乐", false, R.color.color_C29913);
        PodcastTitleListBean podcastTitleListBean5 = new PodcastTitleListBean("今日头条", false, R.color.color_36B285);
        PodcastTitleListBean podcastTitleListBean6 = new PodcastTitleListBean("汽车频道", false, R.color.color_FD3E4A);
        PodcastTitleListBean podcastTitleListBean7 = new PodcastTitleListBean("军事前线", false, R.color.color_text);
        PodcastTitleListBean podcastTitleListBean8 = new PodcastTitleListBean("美女主播", false, R.color.color_C29913);
        PodcastTitleListBean podcastTitleListBean9 = new PodcastTitleListBean("", false, R.color.color_C29913);
        PodcastTitleListBean podcastTitleListBean10 = new PodcastTitleListBean("", false, R.color.color_C29913);
        podcastTitleListBeanList = new ArrayList<>();
        podcastTitleListBeanList.add(podcastTitleListBean01);
        podcastTitleListBeanList.add(podcastTitleListBean02);
        podcastTitleListBeanList.add(podcastTitleListBean);
        podcastTitleListBeanList.add(podcastTitleListBean1);
        podcastTitleListBeanList.add(podcastTitleListBean2);
        podcastTitleListBeanList.add(podcastTitleListBean3);
        podcastTitleListBeanList.add(podcastTitleListBean4);
        podcastTitleListBeanList.add(podcastTitleListBean5);
        podcastTitleListBeanList.add(podcastTitleListBean6);
        podcastTitleListBeanList.add(podcastTitleListBean7);
        podcastTitleListBeanList.add(podcastTitleListBean8);
        podcastTitleListBeanList.add(podcastTitleListBean9);
        podcastTitleListBeanList.add(podcastTitleListBean10);
        //本周上新
        ArrayList<Integer> newMusicdata = new ArrayList<>();
        newMusicdata.add(0);
        newMusicdata.add(0);
        newMusicdata.add(0);
        newMusicdata.add(0);
        newMusicdata.add(0);
        newMusicdata.add(0);
        podcastTitleListAdapter.getItems().addAll(podcastTitleListBeanList);
        binding.rvPodcastTitle.setAdapter(podcastTitleListAdapter);
        linearLayoutManager.scrollToPositionWithOffset(0, PixelUtil.px2dp(_mActivity,-220));
        binding.rvWeekNew.setLayoutManager(new GridLayoutManager(_mActivity,2));
        FeaturedHomeHotPodcastListAdapter featuredHomeHotPodcastListAdapter = new FeaturedHomeHotPodcastListAdapter(_mActivity);
        featuredHomeHotPodcastListAdapter.getItems().addAll(newMusicdata);
        binding.rvWeekNew.setAdapter(featuredHomeHotPodcastListAdapter);
    }

    @Override
    protected void initListener() {
        binding.ivFmSousuo.setOnClickListener(this);
        binding.rvPodcastTitle.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                 firstVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition(); //获取第一个可见坐标
                int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition();

                maindx = dx;
                MLog.e("dx:"+dx);
                if (dx>0){

                    centerPosition = lastVisibleItemPosition-2;
                }else {
                    centerPosition = firstVisibleItemPosition+2;
                }
                new Handler().post(new Runnable() {
                    @Override
                    public void run() {

                        for (PodcastTitleListBean podcastTitleListBean : podcastTitleListBeanList) {
                            podcastTitleListBean.setCheck(false);
                        }
                        podcastTitleListBeanList.get(centerPosition).setCheck(true);


                        podcastTitleListAdapter.notifyDataSetChanged();

                    }
                });

            }

            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                MLog.e("newState:"+newState);
                MLog.e("maindx:"+maindx);
                if (newState==0){
                    binding.llAtWill.setBackground(getResources().getDrawable(podcastTitleListBeanList.get(centerPosition).getBgColor()));
                    MLog.e("firstVisibleItemPosition:"+firstVisibleItemPosition);
                    linearLayoutManager.scrollToPositionWithOffset(firstVisibleItemPosition,PixelUtil.dp2px(_mActivity,-50));
//                    if (maindx>0){
////                        binding.rvPodcastTitle.smoothScrollToPosition(firstVisibleItemPosition+1);
//                    }else {
//
////                        binding.rvPodcastTitle.smoothScrollToPosition(firstVisibleItemPosition);
//                        binding.llAtWill.setBackground(getResources().getDrawable(podcastTitleListBeanList.get(firstVisibleItemPosition).getBgColor()));
//
//                    }

                }
            }
        });
        binding.seekbarBoke.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                binding.tvNum1.setText(progress+50.88+"");
                binding.tvNum2.setText(progress+50.88+"");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

//                MediaPlayerUtils.getInstance().seekTo(progress);
            }
        });
    }

    public static PodcastFragment newInstance() {

        Bundle args = new Bundle();

        PodcastFragment fragment = new PodcastFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_fm_sousuo:
                if (View.GONE==binding.llFmKongzhi.getVisibility()){
                    binding.llFmKongzhi.setVisibility(View.VISIBLE);
                    binding.llFmKongzhiGundong.setVisibility(View.GONE);
                }else {
                    binding.llFmKongzhi.setVisibility(View.GONE);
                    binding.llFmKongzhiGundong.setVisibility(View.VISIBLE);
                }

                break;
        }
    }
}